package com.dykj.fireFighting.web.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dykj.fireFighting.api.base.enterprise.entity.Enterprise;
import com.dykj.fireFighting.api.base.enterprise.service.IEnterpriseService;
import com.dykj.fireFighting.api.base.rescue.entity.RescueInfo;
import com.dykj.fireFighting.api.base.rescue.service.IRescueInfoService;
import com.dykj.fireFighting.api.base.video.entity.Video;
import com.dykj.fireFighting.api.base.video.vo.VideoPlayVo;
import com.dykj.fireFighting.api.socialUnit.devHis.entity.CrtVo;
import com.dykj.fireFighting.api.socialUnit.devHis.entity.DevHis;
import com.dykj.fireFighting.api.socialUnit.devHis.entity.DevHisFire;
import com.dykj.fireFighting.api.socialUnit.devHis.entity.SuspectedFire;
import com.dykj.fireFighting.api.socialUnit.devHis.service.IDevHisFireService;
import com.dykj.fireFighting.api.socialUnit.devHis.service.IDevHisService;
import com.dykj.fireFighting.api.socialUnit.devicemanger.entity.Device;
import com.dykj.fireFighting.api.socialUnit.devicemanger.entity.DeviceRescue;
import com.dykj.fireFighting.api.socialUnit.devicemanger.entity.DeviceTradition;
import com.dykj.fireFighting.api.socialUnit.devicemanger.service.IDeviceRescueService;
import com.dykj.fireFighting.api.socialUnit.devicemanger.service.IDeviceService;
import com.dykj.fireFighting.api.socialUnit.devicemanger.service.IDeviceTraditionService;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.starter.redisson.service.RedisService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author fgc
 * @Date 2021/7/28 15:41
 * @ Description：火警通知数据接口
 */
@RestController
@RequestMapping("/fireAlarmNotice")
public class FireAlarmNoticController {
    @DubboReference
    private IDevHisFireService devHisFireService;
    @DubboReference
    private IDeviceService deviceService;
    @DubboReference
    private IEnterpriseService enterpriseService;
    @DubboReference
    private IRescueInfoService iRescueInfoService;
    @DubboReference
    private IDeviceTraditionService deviceTraditionService;
    @DubboReference
    private IDeviceRescueService deviceRescueService;
    @DubboReference
    private IDevHisService devHisService;
    @Autowired
    private RedisService redisService;

    @GetMapping("/getFires/{ids}")
    public CommonResult<List<DevHisFire>> getByIds(@PathVariable("ids") String ids){
        List<DevHisFire> devHisFires = devHisFireService.queryByIds(ids);

        return CommonResult.success(devHisFires);
    }
    @GetMapping("/updateRead")
    public CommonResult updateRead(String id,String read){
        DevHis devHis=new DevHis();
        devHis.setId(id);
        devHis.setIsRead(read);
        int i=devHisService.updateDevHis(devHis);
        return CommonResult.success("成功");
    }
    @GetMapping("/getYsFires/{ids}")
    public CommonResult<List<DevHisFire>>getYsFires(@PathVariable("ids") String ids){
        SuspectedFire suspectedFire = devHisFireService.getById(ids);
        List<DevHisFire> devHisFires = devHisFireService.queryByIds(suspectedFire.getDevHisIds());
        suspectedFire.setIsConfirm("1");
        devHisFireService.updateSuspectedFire(suspectedFire);
        return CommonResult.success(devHisFires);
    }
    @GetMapping("/getDeviceUnit/{relationId}")
    public CommonResult getDeviceUnit(@PathVariable("relationId") String relationId){
        Device devicedetails = (Device)deviceService.getDevicedetails(relationId);
        Enterprise unit = enterpriseService.getById(devicedetails.getUnitId());
        //获取救援单位
        RescueInfo resuse = iRescueInfoService.getById(unit.getResuseId());
        //获取救援医院
        RescueInfo hospital = iRescueInfoService.getById(unit.getHospitalId());
        Map<String,Object> result=new HashMap<>();
        result.put("unit",unit);
        result.put("resuse",resuse);
        result.put("hospital",hospital);
        return CommonResult.success(result);
    }
    @GetMapping("/getDeviceVideo/{relationId}")
    public CommonResult<VideoPlayVo> getDeviceVideo(@PathVariable("relationId") String relationId){
        Device devicedetails = (Device)deviceService.getDevicedetails(relationId);
        return CommonResult.success(deviceService.getVideoByDeviceId(devicedetails.getId()));
    }
    @GetMapping("/getCrt/{relationId}")
    public CommonResult<CrtVo> getCrt(@PathVariable("relationId") String relationId){
        Device devicedetails = (Device)deviceService.getDevicedetails(relationId);
        return CommonResult.success(deviceService.getCrt(devicedetails.getId()));
    }
    /**
     * 获取设备管理-智能设备详情
     */
    @GetMapping("/details/{relationId}")
    public CommonResult<Object> details(@PathVariable("relationId") String relationId){
        Object device = deviceService.getDeviceByRelationId(relationId);
        return CommonResult.success(device);
    }

    /**
     * 设备管理-设备详情
     *
     * @return
     */
    @AutoLog(value = "设备管理-智能设备-智能设备详情")
    @ApiOperation(value = "设备管理-智能设备详情", notes = "设备管理-智能设备详情")
    @GetMapping("/device/detailAll")
    public CommonResult getDeviceDetail(String id, Integer tableType){
        HashMap<String, Object> map = new HashMap<>(8);
        //类型 0智能设备  1传统设备 2救援设备
        if (tableType == 0) {
            Device devicedetails = deviceService.bigDataDevice(id);
            String s = (String)redisService.get(devicedetails.getRelationId());
            if(StringUtils.isNotBlank(s)){//判断缓存中是否存在
                JSONObject o = JSON.parseObject(s);
                Object oo=o.get("status");
                if(null!=oo){
                    devicedetails.setStatus(oo.toString());
                }
            }
            map.put("zhineng",devicedetails);
        }else if (tableType == 1) {
            DeviceTradition deviceTradition = deviceTraditionService.getById(id);
            map.put("chuantong",deviceTradition);
        }else if (tableType == 2) {
            DeviceRescue deviceRescue = deviceRescueService.getById(id);
            map.put("jiuyuan",deviceRescue);
        }
        return CommonResult.success(map);
    }

    /**
     * 获取设备管理-智能设备信息
     */
    @AutoLog(value = "设备管理-智能设备-获取设备管理-智能设备信息")
    @ApiOperation(value = "获取设备管理-智能设备信息", notes = "获取设备管理-智能设备信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/device/info/{id}")
    //@RequiresPermissions("device/info")
    public CommonResult<Device> deviceInfo(@PathVariable("id") String id){
        Device device = deviceService.getById(id);
        return CommonResult.success(device);
    }
    /**
     * 获取设备管理-传统设备表信息
     */
    @AutoLog(value = "设备管理-传统设备表-获取设备管理-传统设备表信息")
    @ApiOperation(value = "获取设备管理-传统设备表信息", notes = "获取设备管理-传统设备表信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/deviceTradition/info/{id}")
//    @RequiresPermissions("deviceTradition/info")
    public CommonResult<DeviceTradition> deviceTraditionInfo(@PathVariable("id") String id){
        DeviceTradition deviceTradition = deviceTraditionService.getById(id);
        return CommonResult.success(deviceTradition);
    }
    @GetMapping("/getDeviceVideoByDevId/{deviceId}")
    public CommonResult<VideoPlayVo> getDeviceVideoByDevId(@PathVariable("deviceId") String deviceId){
        return CommonResult.success(deviceService.getVideoByDeviceId(deviceId));
    }
}
